<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>State Template</title>
<link href="../docs-assets/Breadcrumbs.css" rel="stylesheet" rev="stylesheet" type="text/css">
		<meta name="viewport" content="width=device-width initial-scale=1">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="Content-Language" content="en-gb">

<link href="../docs-assets/Contents.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Progress.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Navigation.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Fonts.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Base.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Colours.css" rel="stylesheet" rev="stylesheet" type="text/css">
		
	</head>
	<body class="commentary-font">
		<nav role="navigation">
		<h1><a href="../index.html"><img src="../docs-assets/Inform.png" height=72> </a></h1>
<ul><li><a href="../index.html">home</a></li>
</ul><h2>Compiler</h2><ul>
<li><a href="../structure.html">structure</a></li>
<li><a href="../inbuildn.html">inbuild</a></li>
<li><a href="../inform7n.html">inform7</a></li>
<li><a href="../intern.html">inter</a></li>
<li><a href="../services.html">services</a></li>
<li><a href="../secrets.html">secrets</a></li>
</ul><h2>Other Tools</h2><ul>
<li><a href="../inblorbn.html">inblorb</a></li>
<li><a href="../inform6.html">inform6</a></li>
<li><a href="../inpolicyn.html">inpolicy</a></li>
</ul><h2>Resources</h2><ul>
<li><a href="../extensions.html">extensions</a></li>
<li><a href="../kits.html">kits</a></li>
</ul><h2>Repository</h2><ul>
<li><a href="https://github.com/ganelson/inform"><img src="../docs-assets/github.png" height=0> github</a></li>
</ul><h2>Related Projects</h2><ul>
<li><a href="https://github.com/ganelson/inweb"><img src="../docs-assets/github.png" height=0> inweb</a></li>
<li><a href="https://github.com/ganelson/intest"><img src="../docs-assets/github.png" height=0> intest</a></li>
</ul>
		</nav>
		<main role="main">
		<!-- Weave of 'State Template' generated by inweb -->
<div class="breadcrumbs">
    <ul class="crumbs"><li><a href="../index.html">Home</a></li><li><a href="../extensions.html">Kits</a></li><li><a href="index.html">Architecture16Kit</a></li><li><b>State Template</b></li></ul></div>
<p class="purpose">Saving, restoring, restarting and verifying the program from within itself.</p>

<ul class="toc"><li><a href="sttm2.html#SP1">&#167;1. Environment</a></li><li><a href="sttm2.html#SP2">&#167;2. Verification</a></li><li><a href="sttm2.html#SP3">&#167;3. Save, restore, restart</a></li><li><a href="sttm2.html#SP4">&#167;4. Undo</a></li></ul><hr class="tocbar">

<p class="commentary firstcommentary"><a id="SP1" class="paragraph-anchor"></a><b>&#167;1. Environment.</b>The language "interpreter" here supposes that the eventual program is
running in a VM which is being interpreted, and that may not be the case,
but it's traditional.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_ReportOnInterpreter</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">standard_interpreter</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">"Standard interpreter "</span><span class="plain-syntax">,</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">standard_interpreter</span><span class="plain-syntax">/256, </span><span class="string-syntax">"."</span><span class="plain-syntax">, </span><span class="identifier-syntax">standard_interpreter</span><span class="plain-syntax">%256,</span>
<span class="plain-syntax">            </span><span class="string-syntax">" ("</span><span class="plain-syntax">, </span><span class="identifier-syntax">HDR_TERPNUMBER</span><span class="plain-syntax">-&gt;0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> (</span><span class="identifier-syntax">char</span><span class="plain-syntax">) </span><span class="identifier-syntax">HDR_TERPVERSION</span><span class="plain-syntax">-&gt;0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">")^"</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">"Interpreter "</span><span class="plain-syntax">, </span><span class="identifier-syntax">HDR_TERPNUMBER</span><span class="plain-syntax">-&gt;0, </span><span class="string-syntax">" Version "</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> (</span><span class="identifier-syntax">char</span><span class="plain-syntax">) </span><span class="identifier-syntax">HDR_TERPVERSION</span><span class="plain-syntax">-&gt;0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">"^"</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP2" class="paragraph-anchor"></a><b>&#167;2. Verification.</b>This verifies that the current story file is intact.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Verify</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">verify</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">Vmaybe</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rfalse</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">Vmaybe</span><span class="plain-syntax">; </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP3" class="paragraph-anchor"></a><b>&#167;3. Save, restore, restart.</b>Restart does what it says: restarts the program as if it had just loaded for
the first time.
</p>

<p class="commentary"><span class="extract"><span class="extract-syntax">VM_Save()</span></span> attempts to save the current state of the program to a file, and
returns 0 if this fails, 1 if this succeeds, or 2 if in fact a restore has
just succeeded. (A successful restoration should resume execution where the
save succeeded, but we want to distinguish those cases.)
</p>

<p class="commentary"><span class="extract"><span class="extract-syntax">VM_Restore()</span></span> pretends to return <span class="extract"><span class="extract-syntax">true</span></span> or <span class="extract"><span class="extract-syntax">false</span></span> according to whether or not
it succeeds, but in fact it can only return <span class="extract"><span class="extract-syntax">false</span></span> to indicate failure, because
a successful restoration means that execution has transferred to <span class="extract"><span class="extract-syntax">VM_Save</span></span>
instead.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Restart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">restart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Restore</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">restore</span><span class="plain-syntax"> </span><span class="identifier-syntax">RMaybe</span><span class="plain-syntax">; </span><span class="reserved-syntax">rfalse</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">RMaybe</span><span class="plain-syntax">; </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Save</span><span class="plain-syntax"> </span><span class="identifier-syntax">flag</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="reserved-syntax">save</span><span class="plain-syntax"> -&gt; </span><span class="identifier-syntax">flag</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">flag</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP4" class="paragraph-anchor"></a><b>&#167;4. Undo.</b>These also simply wrap the relevant opcodes.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Undo</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">restore_undo</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_Save_Undo</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">save_undo</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">result_code</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<nav role="progress"><div class="progresscontainer">
    <ul class="progressbar"><li class="progressprev"><a href="sttm.html">&#10094;</a></li><li class="progresssection"><a href="cptm.html">cptm</a></li><li class="progresssection"><a href="sttm.html">sttm</a></li><li class="progresscurrent">sttm2</li><li class="progresssection"><a href="mttm.html">mttm</a></li><li class="progresssection"><a href="cst.html">cst</a></li><li class="progresssection"><a href="iot.html">iot</a></li><li class="progresssection"><a href="fiot.html">fiot</a></li><li class="progresssection"><a href="vntm.html">vntm</a></li><li class="progressnext"><a href="mttm.html">&#10095;</a></li></ul></div>
</nav><!-- End of weave -->

		</main>
	</body>
</html>

